<?php
/**
 * Created by program
 * Class CommentService
 * Description:评论服务类
 * @package app\common\service
 * Author:sunnier
 * Email:xiaoyao_xiao@126.com
 * 2020-11-18 14:36
 */
namespace app\common\service;

class CommentService extends BaseService
{
    protected $commentModel;

    public function __construct()
    {
        parent::__construct();
        $this->commentModel = model('common/Comment');
    }

    /**
     * @param array $where
     * @param int $page
     * @param int $listRow
     * @param string $order
     * @return array
     * Description:获取列表
     * Author:sunnier
     * Email:xiaoyao_xiao@126.com
     * 2020-11-18 14:36
     */
    public function getList($where=[],$page=1,$listRow=10,$order='id ASC'){
        $count=$this->commentModel->where($where)->count();
        if($count>0){
            $list=$this->commentModel
                ->field('id,content')->where($where)
                ->order($order)->page($page,$listRow)->select();
            return [
                'status'=>1,
                'msg'=>'获取成功！',
                'data'=>$list
            ];
        }else{
            $list=[];
            return [
                'status'=>1,
                'msg'=>'没数据了',
                'data'=>$list
            ];
        }
    }

    /**
     * @param $data
     * @return array
     * Description:提交评论
     * Author:sunnier
     * Email:xiaoyao_xiao@126.com
     * 2020-11-27 12:34
     */
    public function addComment($data){
        try {
            if (empty($data['black_id']) || empty($data['content'])) {
                return [
                    'status' => -10000,
                    'msg' => '参数不完整',
                    'data' => []
                ];
            }
            $nowTime = time();
            $commentData = [
                'black_id' => intval($data['black_id']),
                'content' => filterEmoji(strval($data['content'])),
                'create_time' => $nowTime,
                'update_time' => $nowTime
            ];
            $this->commentModel->insert($commentData);
            return [
                'status' => 1,
                'msg' => '提交成功！',
                'data' => []
            ];
        }catch (\Exception $e){
            return [
                'status' => -10000,
                'msg' => '提交失败！',
                'data' => []
            ];
        }
    }
}